package com.sino.controller;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.http.MediaType;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.InputStream;

/**
 * @title: ResourseLoadController
 * @Author Qiulei
 * @Date: 2023/11/23 13:25
 *
 * 加载jar包的classPath下文件
 */
@RestController
@RequestMapping("/sino/resourseLoad")
public class ResourseLoadController {
    private static final Logger log = LogManager.getLogger();
    @RequestMapping(value="test",produces = MediaType.APPLICATION_JSON_VALUE)
    public Object testRt() {
        try {
            //方式一:
            InputStream input1 = ClassUtils.getDefaultClassLoader().getResourceAsStream("application.properties");
            log.info(">>>>>>>>>>>>1>>"+input1);
            //方式二:
            InputStream input2 = new ClassPathResource("application.properties").getInputStream();
            log.info(">>>>>>>>>>>2>>>"+input2);
            //方式三:
            ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
            InputStream input3 = resolver.getResources("/application.properties")[0].getInputStream();
            log.info(">>>>>>>>>>>3>>>"+input3);
        }catch (Exception e){
            e.printStackTrace();
        }
        return "加载classpath下文件成功";
    }



}
